package com.badibadi.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.badibadi.activity.PicturePreviewActivity;
import com.badibadi.infos.BrowseAllPhoto_Model;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.Constants;
import com.badibadi.uniclubber.R;
//import com.lidroid.xutils.util.LogUtils;

public class MyViewPageAdapter extends PagerAdapter {

	// int[] pic = { R.drawable.zhaopian, R.drawable.noimage,
	// R.drawable.add2_select };
	List<String> allPhoto_Models;
	Context context;

	List<View> views;

	public MyViewPageAdapter(Context context, List<String> allPhoto_Models) {
		LayoutInflater inflater = LayoutInflater.from(context);
		views = new ArrayList<View>();
		this.allPhoto_Models = allPhoto_Models;
		this.context = context;
//		LogUtils.i("运行了MyViewPageAdapter");
//		LogUtils.i(""+allPhoto_Models.size());
		for (int i = 0; i < allPhoto_Models.size(); i++) {
			View view = inflater.inflate(
					R.layout.picture_preview_adapter_import_layout, null);
			// ImageView iv = (ImageView) view
			// .findViewById(R.id.pic_imageView1);
			// ImageLoader.getInstance().displayImage(Constants.BadiDownImgUrl+"/"+allPhoto_Models,
			// iv, options);
			// ImageLoader.getInstance().displayImage(
			// Constants.BadiDownImgUrl + "/"
			// + allPhoto_Models.get(i).getPath()
			// + Constants.appBGimg, iv, options);
			// LogUtils.d(""+Constants.BadiDownImgUrl + "/"
			// + allPhoto_Models.get(i).getPath()
			// + Constants.appBGimg);
			views.add(view);
		}
	}

	/** 这里返回一个view */
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = views.get(position);
		ImageView iv = (ImageView) view.findViewById(R.id.pic_imageView1);

		// ImageLoader.getInstance().clearDiscCache();
		// ImageLoader.getInstance().displayImage(
		// Constants.BadiDownImgUrl + "/"
		// + allPhoto_Models.get(position).getPath()
		// + Constants.appBGimg, iv, options,new
		// SimpleImageLoadingListener(){
		// @Override
		// public void onLoadingFailed(String imageUri,
		// View view, FailReason failReason) {
		// // TODO Auto-generated method stub
		// String message = null;
		// switch (failReason.getType()) { // 获取图片失败类型
		// case IO_ERROR: // 文件I/O错误
		// message = "Input/Output error";
		// break;
		// case DECODING_ERROR: // 解码错误
		// message = "Image can't be decoded";
		// break;
		// case NETWORK_DENIED: // 网络延迟
		// message = "Downloads are denied";
		// break;
		// case OUT_OF_MEMORY: // 内存不足
		// message = "Out Of Memory error";
		// break;
		// case UNKNOWN: // 原因不明
		// message = "Unknown error";
		// break;
		// }
		//
		// // Toast.makeText(ImagePagerActivity.this, message,
		// Toast.LENGTH_SHORT).show();
		// Utils.showMessage(PicturePreviewActivity.this, message);
		//
		// super.onLoadingFailed(imageUri, view, failReason);
		// }
		// });
		// BitmapHelp bitmaphelp = new BitmapHelp();
		// bitmaphelp.getBitmapUtils(PicturePreviewActivity.this).displayImage(iv,
		// Constants.BadiDownImgUrl + "/"
		// + allPhoto_Models.get(position).getPath()
//		LogUtils.d("101=");
		// + Constants.appBGimg);
		if (allPhoto_Models.get(position).startsWith("http")) {
			// bitmaphelp.displayImage(context, iv,
			// allPhoto_Models.get(position));
			new BitmapHelp().displayImage(context, iv,
					allPhoto_Models.get(position));
//			LogUtils.d("106=" + Constants.BadiDownImgUrl + "/"
//					+ allPhoto_Models.get(position));
		} else {

			if (allPhoto_Models.get(position).startsWith("//Public/Uploads/") || allPhoto_Models.get(position).startsWith("/Public/Uploads/")) {
//				LogUtils.d("109=" + Constants.BadiDownImgUrl + "/"
//						+ allPhoto_Models.get(position));
				new BitmapHelp().displayImage(
						context,
						iv,
						Constants.BadiDownImgUrl + "/"
								+ allPhoto_Models.get(position));
			} else {
//				LogUtils.d("115=" + Constants.BadiDownImgUrl + "/"
//						+ allPhoto_Models.get(position));
				new BitmapHelp().displayImage(
						context,
						iv,
						Constants.BadiWebImgUrl + "/"
								+ allPhoto_Models.get(position));
			}

			// bitmaphelp.displayImage(context, iv,
			// Constants.BadiDownImgUrl+"/"+allPhoto_Models.get(position));

		}

		((ViewPager) container).addView(views.get(position));
//		LogUtils.d("" + "加载了第" + position + "涨》" + "/"
//				+ allPhoto_Models.get(position));
		return views.get(position);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);// views.get(position)
//		LogUtils.d("destroyItem");
	}

	@Override
	public int getCount() {
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// LogUtils.d("isViewFromObject");
		return arg0 == arg1;
	}

}